<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class m_user extends CI_Model{
    public function __construct() {
        parent::__construct();
    }
    
    function index() {
        
    }
    
    function get_all_user(){
        $sql = "SELECT * FROM user ORDER BY id_user";
        $query = $this->db->query($sql);

        if ($query) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return false;
        }
    }
    
    function select_user_byId($param) {
        $sql = "SELECT * FROM user WHERE id_user = ?";
        $query = $this->db->query($sql, $param);

        if ($query) {
            $result = $query->row_array();
            return $result;
        } else {
            return false;
        }
    }  
    
    function insert_user($param) {
        $sql = "INSERT INTO user(username_user, fullname_user, "
                . "password_user, fb_acc, twitter_acc, gplus_acc, register_date)"
                . " VALUES(?, ?, ?, ?, ?, ?, now())";
        $query = $this->db->query($sql, $param);
        
        if ($query) {
            return true;
        } else {
            return false;
        }
    }
    
    function update_user($param) {
        if(!isset($param['ava'])):
            $param['ava'] = $_COOKIE['c_ava'];   
        endif;
        $sql = "UPDATE user SET fullname_user = ?, password_user = ?, fb_acc = ?, twitter_acc = ?, gplus_acc = ?, bio = ?, user_ava = ? WHERE id_user = $_COOKIE[c_iduser]";
        $query = $this->db->query($sql, $param);
        
        if($query){
            return TRUE;
        }else{
            return FALSE;   
        }
    }   
    
    function delete_user($param){
        $sql = "DELETE FROM user WHERE id_user = ?";
        $query = $this->db->query($sql, $param);
        
        if($query){
            return TRUE;
        }else{
            return FALSE;
        }
    }
}
